package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.InOutIcuStatisticsDao;
import com.jyxd.web.dao.patient.PatientDao;
import com.jyxd.web.data.basic.InOutIcuStatistics;
import com.jyxd.web.data.dto.InOutPatientDTO;
import com.jyxd.web.util.SliceUpDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class InOutIcuStatisticsService {

    @Autowired
    private InOutIcuStatisticsDao inOutIcuStatisticsDao;
    @Autowired
    private PatientDao patientDao;

    public boolean insert(InOutIcuStatistics inOutIcuStatistics) {
        return inOutIcuStatisticsDao.insert(inOutIcuStatistics);
    }

    public boolean update(InOutIcuStatistics inOutIcuStatistics) {
        return inOutIcuStatisticsDao.update(inOutIcuStatistics);
    }

    public InOutIcuStatistics queryData(String id) {
        return inOutIcuStatisticsDao.queryData(id);
    }

    public List<InOutIcuStatistics> queryList(Map<String, Object> map) {
        return inOutIcuStatisticsDao.queryList(map);
    }

    public int queryNum(Map<String, Object> map) {
        return inOutIcuStatisticsDao.queryNum(map);
    }

    /**
     * 统计分析--出入科--列表展示统计（按天）
     *
     * @param map
     * @return
     */
    public List<Map<String, Object>> getListByDay(Map<String, Object> map) {
        return inOutIcuStatisticsDao.getListByDay(map);
    }

    /**
     * 统计分析--出入科--列表展示统计总量（按天）
     *
     * @param map
     * @return
     */
    public List<Map<String, Object>> getTotalByDay(Map<String, Object> map) {
        return inOutIcuStatisticsDao.getTotalByDay(map);
    }

    /**
     * 统计分析--出入科--列表展示统计（按月）
     *
     * @param map
     * @return
     */
    public List<Map<String, Object>> getListByMonth(Map<String, Object> map) {
        return inOutIcuStatisticsDao.getListByMonth(map);
    }

    /**
     * 统计分析--出入科--列表展示统计总量（按月）
     *
     * @param map
     * @return
     */
    public List<Map<String, Object>> getTotalByMonth(Map<String, Object> map) {
        return inOutIcuStatisticsDao.getTotalByMonth(map);
    }

    /**
     * 根据条件查询出入科对象
     *
     * @param map
     * @return
     */
    public List<InOutIcuStatistics> queryDataByDate(Map<String, Object> map) {
        return inOutIcuStatisticsDao.queryDataByDate(map);
    }

    /**
     * 根据条件统计分析--出入科--列表
     *
     * @param map
     * @return
     */
    public List<InOutPatientDTO> getExitpatientNumList(Map<String, Object> map) {
        LinkedList<InOutPatientDTO> list = new LinkedList<>();
        List<String> datetimeList = new ArrayList<>();
        int exitPatientSumCount = 0;
        int transferPatientSumCount = 0;
        int rollOutPatientSumCount = 0;
        int i = 1;

        int status = 0;
        if(map.containsKey("status")){
            status = Integer.valueOf(map.get("status").toString());
        }
        if (map.get("type").toString().equals("按天")) {
            String startTime = map.get("startTime").toString();
            String endTime = map.get("endTime").toString();
            datetimeList = SliceUpDateUtil.sliceUpDateRange(startTime, endTime, 3);
            //现有患者 判断 flag=1   且enter_time<=当前时间的
            //转入患者  判断flag=1  且enter_time =查询时间
            //转出患者  判断flag=0  且exit_time =查询时间
            for (String datatime : datetimeList) {
                InOutPatientDTO dto = new InOutPatientDTO();
                dto.setDataTime(datatime);
                int exitPatientSum = patientDao.getExitPatientNumMultiDept(datatime, "day",status);
                int transferPatientSum = patientDao.getTransferPatientNumMultiDept(datatime, "day",status);
                int rollOutPatientSum = patientDao.getRollOutPatientNumMultiDept(datatime, "day", null,status);
                if (i == datetimeList.size()) exitPatientSumCount = exitPatientSum;
                i += 1;

                transferPatientSumCount += transferPatientSum;
                rollOutPatientSumCount += rollOutPatientSum;
                dto.setExitPatientSum(exitPatientSum + "");
                dto.setTransferPatientSum(transferPatientSum + "");
                dto.setRollOutPatientSum(rollOutPatientSum + "");
                list.add(dto);
            }


        } else if (map.get("type").toString().equals("按月")) {
            String year = (String) map.get("year");
            datetimeList = SliceUpDateUtil.sliceUpDateRange(year + "-01", year + "-12", 2);
            for (String datatime : datetimeList) {
                InOutPatientDTO dto = new InOutPatientDTO();
                dto.setDataTime(datatime);
                int exitPatientSum = patientDao.getExitPatientNumMultiDept(datatime, "month",status);
                int transferPatientSum = patientDao.getTransferPatientNumMultiDept(datatime, "month",status);
                int rollOutPatientSum = patientDao.getRollOutPatientNumMultiDept(datatime, "month", null,status);
                if (i == datetimeList.size()) exitPatientSumCount = exitPatientSum;
                i += 1;
                transferPatientSumCount += transferPatientSum;
                rollOutPatientSumCount += rollOutPatientSum;
                dto.setExitPatientSum(exitPatientSum + "");
                dto.setTransferPatientSum(transferPatientSum + "");
                dto.setRollOutPatientSum(rollOutPatientSum + "");
                list.add(dto);
            }

        }
        InOutPatientDTO sumdto = new InOutPatientDTO();
        sumdto.setDataTime("总计");
        sumdto.setExitPatientSum(exitPatientSumCount + "");
        sumdto.setTransferPatientSum(transferPatientSumCount + "");
        sumdto.setRollOutPatientSum(rollOutPatientSumCount + "");
        list.addFirst(sumdto);
        return list;
    }
}
